package com.youths.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youths.dao.ExpertInfoMapper;
import com.youths.dao.LabelAdminMapper;
import com.youths.dao.LabelConsumerMapper;
import com.youths.domain.LabelAdmin;
import com.youths.domain.LabelConsumer;
import com.youths.service.ConsumerInfoService;
import com.youths.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ConsumerInfoServiceImpl implements ConsumerInfoService {

    @Autowired(required = false)
    LabelConsumerMapper labelConsumerMapper;

    @Autowired(required = false)
    LabelAdminMapper labelAdminMapper;

    @Autowired(required = false)
    ExpertInfoMapper expertInfoMapper;

    @Override
    public PageInfo getConsumerInfoList(Integer pageNum, Integer id, Integer type, String name, String account) {
        PageHelper.startPage(pageNum,10);
        List<LabelConsumer> consumers = labelConsumerMapper.getList(id,type,name,account);
        PageInfo<LabelConsumer> consumerPageInfo = new PageInfo<>(consumers);
        return consumerPageInfo;
    }

    @Override
    public PageInfo getAdminInfoList(Integer pageNum, Integer type, String name, String account) {
        PageHelper.startPage(pageNum,10);
        List<LabelAdmin> admin = labelAdminMapper.getList(null,type,name,account);
        PageInfo<LabelAdmin> adminPageInfo = new PageInfo<>(admin);
        return adminPageInfo;
    }

    @Override
    @Transactional
    public String resetAdminPassword(Integer adminId) {
        List<LabelAdmin> admins = labelAdminMapper.getList(adminId,null,null,null);
        if(admins.size()>0){
            String passwordMD5 = MD5Util.MD5("youths" + admins.get(0).getLoginAccount().substring(5,11));
            Integer result = labelAdminMapper.updatePassword(adminId,passwordMD5);
            if(result == 1){
                return "success";
            }
        }
        return "fail";
    }

    @Override
    @Transactional
    public String updatePower(Integer adminId, Integer type) {
        Integer result = 0;
        if(type == 1){
            result = labelAdminMapper.updatePower(adminId,2);
        }else if(type == 2){
            result = labelAdminMapper.updatePower(adminId,1);
        }
        if(result == 1){
            return "success";
        }
        return "fail";
    }

    @Override
    @Transactional
    public String updateState(Integer adminId, Integer type) {
        Integer result = 0;
        if(type == 1){
            result = labelAdminMapper.updateState(adminId,0);
        }else if(type == 2){
            result = labelAdminMapper.updateState(adminId,1);
        }
        if(result == 1){
            return "success";
        }
        return "fail";
    }

    @Override
    @Transactional
    public String resetConsumerPassword(Integer consumerId) {
        List<LabelConsumer> consumers = labelConsumerMapper.getList(consumerId,null,null,null);
        if(consumers.size()>0){
            String passwordMD5 = MD5Util.MD5("youths" + consumers.get(0).getConsumerPhone().substring(5,11));
            Integer result = labelConsumerMapper.updatePassword(consumerId,passwordMD5);
            if(result == 1){
                return "success";
            }
        }
        return "fail";
    }

    @Override
    @Transactional
    public String changeAdminPassword(String account, String password, String newPass) {
        String passwordMD5 = MD5Util.MD5("youths"+password);
        List<LabelAdmin> admins = labelAdminMapper.login(account,passwordMD5);
        if(admins.size()>0){
            passwordMD5 = MD5Util.MD5("youths"+newPass);
            Integer result = labelAdminMapper.updatePassword(admins.get(0).getAdminId(),passwordMD5);
            if(result == 1){
                return "success";
            }else{
                return "fail";
            }
        }
        return "different";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createExpert(String account, String name, String company, String title) {
        String passwordMD5 = MD5Util.MD5("youths"+account.substring(5,11));
        try{
            LabelConsumer labelConsumer = new LabelConsumer();
            labelConsumer.setConsumerName(name);
            labelConsumer.setConsumerPhone(account);
            labelConsumer.setPassword(passwordMD5);
            labelConsumer.setKind(1);
            labelConsumerMapper.createAccount(labelConsumer);
            if(labelConsumer.getConsumerId()>0){
                Integer result = expertInfoMapper.insertExpert(labelConsumer.getConsumerId(),name,company,title);
                if(result == 1){
                    return "success";
                }
            }
            return "fail";
        }catch (Exception e){
            return "fail";
        }
    }
}
